* ======================================
* -------- Max. eff. Candidates --------
* ======================================


* Politicians
* ===========

	use $dta\PanelPoliticians.dta, clear
	
	* Restrict to immigrants
	rename Polit_id idPers	
	rename Polit_Run cand
	rename Polit_knr knr
	merge m:1 idPers using $dta/ImportPers.dta, nogen keep(master matched) ///
		keepusing(immigrant birthcountry)
	keep if immigrant == 1

	* Share of gt's total candidacies in municipality mt
	bysort birthcountry knr year: egen nCand_gmt = sum(cand)
	bysort birthcountry year: egen nCand_gt = sum(cand)
	gen pCand_gmt = nCand_gmt/nCand_gt

	collapse pCand_gmt, by(birthcountry knr year)

	tempfile candidates
	save `candidates'


	
* Population at gmt level
* =======================
	use $dta\Population.dta, clear
	merge m:1 idPers using $dta\NetworkImm2.dta, keep(matched) nogen ///
		keepusing(Imm2net)

	* Compute n's
	bysort Imm2net knr year: 		gen n_gomt = _N
	bysort year Imm2net: 			gen n_got = _N
	bysort year birthcountry knr: 	gen n_gmt = _N
	bysort year birthcountry: 		gen n_gt = _N

	* Compute efficiency (fraction inside per gomt)
	gen eff_gomt = n_gomt/n_got

	* Import political candidates
	merge m:1 birthcountry knr year using `candidates', keep(master matched) nogen
	replace pCand_gmt = 0 if pCand_gmt == .


	* Generate estimation variables 
	gen s_gmt = n_gmt/n_gt		 	 // share of group's pop in municipality m
	gen s2_gmt = s_gmt^2 			 // second order
	gen s3_gmt = s_gmt^3 			 // third order
	gen s4_gmt = s_gmt^4 			 // fourth order
	gen pCand100_gmt = pCand_gmt*100 // percentize
	egen numbc = group(birthcountry) // numeric birthcountry
	xtset numbc
	
	
	
* Estimate models
* ===============

	estimates clear

	preserve
		drop if n_gt < 1000 // Immigrant population restriction
		drop if n_got < 10	// Immigrant-occupation population restriction

		* Compute max efficiency and collapse to gomt level
		bysort knr birthcountry year: egen maxeff_gmt = max(eff_gomt)
		collapse pCand100_gmt maxeff s*_gmt, by(knr numbc year)
			
		* standardize maxeff
		qui sum maxeff
		local mean = r(mean)
		local sd = r(sd)
		gen maxeff_std = (maxeff - `mean') / `sd'
	
		* ymean
		sum pCand100_gmt
		local meany = string(r(mean), "%9.2fc") + " \%"
	
		* Regressions
		eststo: xtreg pCand100_gmt maxeff_std, fe cluster(numbc)
		quietly estadd local poly "-", replace
		quietly estadd local ymean "`meany'", replace
	
		eststo: xtreg pCand100_gmt maxeff_std s_gmt, fe cluster(numbc)
		quietly estadd local poly "Linear", replace
		quietly estadd local ymean "`meany'", replace
		
		eststo: xtreg pCand100_gmt maxeff_std s_gmt s2_gmt, fe cluster(numbc)
		quietly estadd local poly "Quadratic", replace
		quietly estadd local ymean "`meany'", replace
		
		eststo: xtreg pCand100_gmt maxeff_std s_gmt s2_gmt s3_gmt, fe cluster(numbc)
		quietly estadd local poly "Cubic", replace
		quietly estadd local ymean "`meany'", replace
	
		eststo: xtreg pCand100_gmt maxeff_std s_gmt s2_gmt s3_gmt s4_gmt, fe cluster(numbc)
		quietly estadd local poly "Quartic", replace
		quietly estadd local ymean "`meany'", replace
	restore
	


* Export table
* ============

	esttab * ///
		using $tbls\MaxeffCandidacies.tex, replace  style(tex) ///
		prehead("\begin{tabular}{l*{6}{c}} \hline\hline \\") ///
		posthead("\hline \\") ///
		fragment ///
		nomtitles ///
		label se b(%9.3f) se(%9.3f) nostar ///
		coeflabels(maxeff_std "Maximum efficiency (std.)") ///
		drop(_cons s*_gmt) noomitted nobaselevels ///
		nonotes stats(poly N N_clust ymean, labels( ///
			"Population share polynomial" "Observations" ///
			"Clusters" "Mean dependent variable") ///
			fmt(%15.0fc %15.0fc %15.0fc)) ///
		substitute(\_ _) ///
		prefoot("\hline") ///
		postfoot("\hline\hline \\ \end{tabular}")

	